﻿#region
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using System.Linq;
using EndlessDownloader;
using EndlessDownloader.Engine;

#endregion

namespace EndlessDownloaderEx
{
    public partial class DownloadViewer : UserControl
    {
        public DownloadViewer()
        {
            InitializeComponent();
        }

        private readonly List<ButtonGrid> _grids = new List<ButtonGrid>();

        public void AddPicture(Picture picture)
        {
            ButtonGrid grid = _grids.Find(x => x.SuperNumber == picture.SuperNumber);

            if (grid == null)
            {
                AddTabCore(picture.SuperNumber);
                grid = _grids.Find(x => x.SuperNumber == picture.SuperNumber);
            }

            grid.SetPicture(picture);
        }

        private void AddTabCore(int superNumber)
        {
            this.InvokeIfNeeded(() =>
                                    {
                                        ButtonGrid buttonGrid = new ButtonGrid();
                                        buttonGrid.SuperNumber = superNumber;
                                        buttonGrid.Location = new Point(6, 6);
                                        buttonGrid.Size = new Size(600, 800);
                                        buttonGrid.ButtonClicked += ButtonGrid_ButtonClicked;
                                        _grids.Add(buttonGrid);

                                        TabPage tabPage = new TabPage(string.Format("{0} ~ {1}", superNumber * 300 + 1, superNumber * 300 + 300));
                                        tabPage.Padding = new Padding(3);
                                        tabPage.Controls.Add(buttonGrid);

                                        tbcTab.TabPages.Add(tabPage);
                                    });
        }

        public void SetAsCompleted(Picture picture)
        {
            AddPicture(picture);

            this.InvokeIfNeeded(() =>
                          {
                              _grids.Find(x => x.SuperNumber == picture.SuperNumber).SetAsCompleted(picture.Number);
                          });
        }

        private void ButtonGrid_ButtonClicked(object sender, ButtonGrid.ButtonClickedEventArgs e)
        {
            OnButtonClicked(e.SavedPath);
        }

        #region ButtonClicked event things for C# 3.0
        public event EventHandler<ButtonClickedEventArgs> ButtonClicked;

        protected virtual void OnButtonClicked(ButtonClickedEventArgs e)
        {
            if (ButtonClicked != null)
                ButtonClicked(this, e);
        }

        protected virtual void OnButtonClicked(string savedPath)
        {
            OnButtonClicked(new ButtonClickedEventArgs(savedPath));
        }

        protected virtual ButtonClickedEventArgs OnButtonClickedWithReturn(string savedPath)
        {
            ButtonClickedEventArgs args = new ButtonClickedEventArgs(savedPath);
            OnButtonClicked(args);

            return args;
        }

        public class ButtonClickedEventArgs : EventArgs
        {
            public string SavedPath { get; set; }

            public ButtonClickedEventArgs()
            {
            }

            public ButtonClickedEventArgs(string savedPath)
            {
                SavedPath = savedPath;
            }
        }
        #endregion
    }
}